/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once
#include <map>
#include <string>

#include "SDL.h"
#include "IInput.h"
/**
 * Definitions
 */
#define MAX_KEYS 333
#define MAX_BUTTONS 4
#define MAX_JOYSTICKBUTTONS 10
#define NEXT_TIME 400

/**
=============================
A key representation
=============================
*/
typedef struct {
    bool isDown;
    bool Released;

    long TimeStamp;
    long NextTime;
} Key;


/**
 * Input subsystem
 */
class Input : public IInput
{
public:
    Input(void);
    /** Initialize the input system */
    int Init();
    /** Poll for events */
	void Update(float dt);
    /** Shutdown */
	void Shutdown();

    /*----------------------------
        Handle Joystick Input
    -----------------------------*/
    /** Test to see if there is a joystick availible, return the count */
    int CheckJoystick();
    /** Enable the joystick module */
    void EnableJoystick();
    /** Check the x-axis */
    int GetJoystickX();
    /** Check the y-axis */
    int GetJoystickY();
    /** Test to see if a button is down */
    int IsJoystickButtonDown(int button, bool stamp=false);

    /*----------------------------
        Handle the mouse 
    -----------------------------*/

    /** Get the mouse x-pos */
    int GetMouseX();
    /** Get the mouse y-pos */
	int GetMouseY();

    /** Get the change in x */
    int GetDeltaMX();
    /** Get the change in y */
    int GetDeltaMY();
    /** Test to see if a mouse button is down */
    bool IsButtonDown( int button, bool stamp=false );
    /** Test to see if a button is released */
    bool ButtonReleased( int button );

    /*---------------------------------
        Handle keyboard 
    ----------------------------------*/

    /** Test to see if a key is down */
    bool IsKeyDown( int key, bool stamp=false );
    /** Test to see if a key is released */
    bool KeyReleased( int key );
public:
    virtual ~Input(void);

private:
    // current event
    SDL_Event*    m_event;
    // the joystick
    SDL_Joystick* m_joystick;
    // keyboard buttons
    Key  key[MAX_KEYS];
    // joystick buttons
    Key  joyStickButtons[MAX_JOYSTICKBUTTONS];
    // mouse buttons
    bool button[MAX_BUTTONS];

    // old mouse x&y
    int m_oldx, m_oldy;
    // current mouse x&y
    int m_x, m_y;

    /*------------------------
        Key Binding functions
    -------------------------*/
    Bind_f bind_f;
    UnBind_f unbind_f;
    UnBindAll_f unbindAll_f;

};
